<?php 
	
function doPages($paramName, $page_size, $thepage, $query_string='', $total=0) {
// 	$paramName: tên tham số để lấy trang hiện tại
//	$page_size: số row/trang
//	$thepage: URL của thẻ a
//	$query_string: tham số truyền vô thêm URL  
//	$total: tổng số row của data
  
  //số trang sẽ hiển thị cùng với Đầu Trước . . . . Sau Cuối
  if($total == 0)
		return;
	$index_limit = 10;

	$query='';

  if(strlen($query_string)>0){
    $query = "&".$query_string;
  }

  //get the current page number example: 3, 4 etc: see above method description
  $current = get_current_page($paramName);

  $total_pages=ceil($total/$page_size);
  if ($total_pages > $index_limit)
		$start=max($current-intval($index_limit/2), 1); 
	else
		$start = 1;
  $end=$start+$index_limit-1; 
	
	
	echo '<div class="pagination pagination-centered"><ul>';

 
   if($start > 1) {
    $i = 1;
    echo "<li><a  title='go to page {$i}' href='{$thepage}?{$paramName}={$i}$query'>Đầu</a></li>";
		// echo "<li><a  href='{$thepage}?{$paramName}={$i}$query'>Đầu</a></li>";
   }

	 if($current==1) {
    echo "<li class='disabled'><a>«</a></li>";
  } else {
    $i = $current-1;
    echo "<li><a href='{$thepage}?{$paramName}={$i}$query'> «</a></li>";
    
  }
	if($current >= $index_limit)	
		echo "<li class='disabled'><a  >...</a></li>";
	
  for ($i = $start; $i <= $end && $i <= $total_pages; $i++){
    if($i==$current) {
    echo "<li class='active'><a  >$i</a></li>";
    } else {
    echo "<li><a  href='{$thepage}?{$paramName}={$i}$query'>$i</a></li>";
    }
  }
	if($total_pages > $index_limit)
		if($current <= $total_pages - $index_limit +1)
			echo "<li class='disabled'><a  >...</a></li>";
	
  if($current < $total_pages) {
    $i = $current+1;
    echo "<li><a href='{$thepage}?{$paramName}={$i}$query'>»</a></li>";
  } else {
    echo "<li class='disabled'><a  >»</a></li>";
  }
	
	 // if($total_pages > $end){
    // $i = $total_pages;
    // echo "<li><a  href='{$thepage}?{$paramName}={$i}$query'>Cuối</a></li>";
  // }
  //if nothing passed to method or zero, then dont print result, else print the total count below:
  if ($total != 0){
    //prints the total result count just below the paging
    echo "<br/>(Trang $current/$total_pages - Total: $total)";
  }
	echo "</ul></div>";
	
}//end of method doPages()

function doPagesRewrite($current, $page_size, $thepage, $query_string='', $total=0) {
// 	$current: page hien tai
//	$page_size: số row/trang
//	$thepage: URL của thẻ a
//	$query_string: tham số truyền vô thêm URL  
//	$total: tổng số row của data
  
  //số trang sẽ hiển thị cùng với Đầu Trước . . . . Sau Cuối
	$paramName = 'p';
	
	if ($current == 0)
		$current++; 
  
  if($total == 0)
		return;
	$index_limit = 10;

	$query='';
	
  if(strlen($query_string)>0){
    $query = "-".$query_string;
  }

  $total_pages=ceil($total/$page_size);
  if ($total_pages > $index_limit)
		$start=max($current-intval($index_limit/2), 1); 
	else
		$start = 1;
  $end=$start+$index_limit-1; 
	
	
	echo '<div class="pagination pagination-centered"><ul>'; 

 
   if($start > 1) {
    $i = 1;
    echo "<li><a  title='go to page {$i}' href='{$thepage}.{$paramName}{$i}$query'>Đầu</a></li>";
		// echo "<li><a  href='{$thepage}?{$paramName}={$i}$query'>Đầu</a></li>";
   }

	 if($current==1) {
    echo "<li class='disabled'><a  >«</a></li>";
  } else {
    $i = $current-1;
    echo "<li><a href='{$thepage}.{$paramName}{$i}$query'> «</a></li>";
    
  }
	if($current >= $index_limit)	
		echo "<li class='disabled'><a  >...</a></li>";
	
  for ($i = $start; $i <= $end && $i <= $total_pages; $i++){
    if($i==$current) {
    echo "<li class='active'><a  >$i</a></li>";
    } else {
    echo "<li><a  href='{$thepage}.{$paramName}{$i}$query'>$i</a></li>";
    }
  }
	if($total_pages > $index_limit)
		if($current <= $total_pages - $index_limit +1)
			echo "<li class='disabled'><a  >...</a></li>";
	
  if($current < $total_pages) {
    $i = $current+1;
    echo "<li><a href='{$thepage}.{$paramName}{$i}$query'>»</a></li>";
  } else {
    echo "<li class='disabled'><a  >»</a></li>";
  }
	
	 // if($total_pages > $end){
    // $i = $total_pages;
    // echo "<li><a  href='{$thepage}?{$paramName}={$i}$query'>Cuối</a></li>";
  // }
  //if nothing passed to method or zero, then dont print result, else print the total count below:
  if ($total != 0){
    //prints the total result count just below the paging
    //echo "<br/>(Trang $current/$total_pages - Total: $total)";
  }
	echo "</ul></div>";
	
}//end of method doPages()
function check_integer($which) {
  if(isset($_REQUEST[$which])){
    if (intval($_REQUEST[$which])>0) {
    //check the paging variable was set or not,
    //if yes then return its number:
    //for example: ?{$paramName}=5, then it will return 5 (integer)
    return intval($_REQUEST[$which]);
    } else {
    return false;
    }
  }
  return false;
}//end of check_integer()

function get_current_page($paramName) {
  if(($var=check_integer($paramName))) {
    //return value of 'page', in support to above method
    return $var;
  } else {
    //return 1, if it wasnt set before, page=1
    return 1;
  }
}//end of method get_current_page()
?>